package stz.flash.main;

import net.rim.device.api.system.Application;
import net.rim.device.api.system.RuntimeStore;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.UiEngine;

public class ScreenHandle {

	public static final long RUNTIME_STORE_KEY_OF_IS_DISPLAYED = 0x65db8400e340a6e5L;//ScreenHandle
	
	public static final int PRIORITY_OPTION_SCREEN = -1;
	public static final int PRIORITY_FLASH_SCREEN = 0;
	
	private static ScreenHandle screenCtrl = new ScreenHandle();
	private static UiEngine uiEngine = UiApplication.getUiApplication();
	private static Screen screen;
	
	private ScreenHandle() {}
	
	public static ScreenHandle getIns(){
		return screenCtrl;
	}
	
	public void setScreen(Screen screen){
		ScreenHandle.screen = screen;
	}
	
	public synchronized void pushScreen(){
		if (screen == null || screen.isDisplayed()) 
			return;
		
		synchronized (Application.getEventLock()) {
			uiEngine.pushGlobalScreen(
					screen, 
					PRIORITY_FLASH_SCREEN, 
					UiApplication.GLOBAL_SHOW_LOWER);
		}
		RuntimeStore.getRuntimeStore().replace(RUNTIME_STORE_KEY_OF_IS_DISPLAYED, new Boolean(true));
	}
	
	public synchronized void popScreen(){
		if (screen.isDisplayed() == false) 
			return;
		
		synchronized (Application.getEventLock()) {
			uiEngine.popScreen(screen);
		}
		RuntimeStore.getRuntimeStore().replace(RUNTIME_STORE_KEY_OF_IS_DISPLAYED, new Boolean(false));
	}
	
	public boolean isDisplayed() {
		if (screen == null) 
			return false;
		return screen.isDisplayed();
	}
}